package com.code516.web;

import com.code516.entity.StudentEntity;
import com.code516.service.StudentService;
import com.code516.util.SendVerificationCode;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;


/**
 * 进社区系统的首页
 * 实现登录注册功能
 * 以及个人中心
 * @author Administrator
 */

@RequestMapping("loginAction")
@Controller
public class LoginAction {

	@Resource
	private StudentService studentService;

	/**
	 * 检查用户是否登录，sessoin失效时跳转到首页
	 * @param session
	 * @return
	 */
	@RequestMapping("checkIsLogin")
	@ResponseBody
	public Map<String, Object> checkIsLogin(HttpSession session){
		Map<String, Object> map = new HashMap<String, Object>();
		StudentEntity studentEntity = (StudentEntity) session.getAttribute("studentEntity");
		if(studentEntity == null){
			map.put("flag", -1);//失效，用户下线
		}else{
			map.put("flag", 1);
			map.put("stuPhone", studentEntity.getStuMobile());
		}
		return map;
	}

	/**
	 * 验证该手机和密码能否实现登录
	 * @param stuMobile
	 * @param password
	 * @return
	 */
	@RequestMapping("checkLogin")
	@ResponseBody
	public Map<String, Object> checkLogin(String stuMobile, String password, HttpSession session){
		Map<String, Object> map = new HashMap<String, Object>();
		StudentEntity studentEntity = studentService.findByMobile(stuMobile);
		if(studentEntity == null){
			map.put("flag", -1); //查无用户，不可登录
		}else if(studentEntity.getStuPass().equals(password)){
			map.put("flag", 1); //可以登录
			session.setAttribute("studentEntity", studentEntity);
		}else{
			map.put("flag", 0);
		}
		return map;
	}


	/**
	 * 验证该手机能否注册,实际上是查询该手机号是否已经存在
	 * @param stuMobile
	 * @return
	 */
	@RequestMapping("checkRegister")
	@ResponseBody
	public Map<String, Object> checkRegister(String stuMobile){
		Map<String, Object> map = new HashMap<String, Object>();
		StudentEntity studentEntity = studentService.findByMobile(stuMobile);
		if(studentEntity == null){
			map.put("flag", 1); //查无用户，可以注册
		}else{
			map.put("flag", -1);//存在该用户，不可注册
		}
		return map;
	}


	/**
	 * 验证该手机能否注册
	 * @param stuMobile
	 * @return
	 */
	@RequestMapping("sendYzm")
	@ResponseBody
	public Map<String, Object> sendYzm(String stuMobile, HttpSession session){
		Map<String, Object> map = new HashMap<String, Object>();
		Random random = new Random();
		int yzm = random.nextInt(899999)+100000;
		session.setAttribute("yzm", yzm);
		int result = SendVerificationCode.sendMSM(stuMobile, String.valueOf(yzm));
		map.put("flag", result); //发送成功时，返回0
		return map;
	}

	/**
	 * 验证该手机能否注册
	 * @param stuMobile
	 * @return
	 */
	@RequestMapping("register")
	@ResponseBody
	public Map<String, Object> register(String stuMobile, String password, String phoneYzm, HttpSession session){
		Map<String, Object> map = new HashMap<String, Object>();
		Integer yzm = (Integer) session.getAttribute("yzm");
		if(yzm==null){
			map.put("flag", -1);//验证码失效
		}else if(String.valueOf(yzm).equals(phoneYzm)){ //验证码没过期并且正确
			int result = studentService.saveStudent(stuMobile, password);
			session.removeAttribute("yzm");//存入是否成功，清空session
			map.put("flag", result);
		}else{
			map.put("flag", -2);//前后台验证码不一致
		}
		return map;
	}


	/**
	 * 1.检查手机是否注册
	 * @param stuMobile
	 * @param session
	 * @return
	 */
	@RequestMapping("checkSendYzm")
	@ResponseBody
	public Map<String, Object> checkSendYzm(String stuMobile, HttpSession session){
		Map<String, Object> map = new HashMap<String, Object>();
		Random random = new Random();
		int yzm = random.nextInt(899999)+100000;
		session.setAttribute("forgetSendYzm", yzm);
		int result = SendVerificationCode.sendMSM(stuMobile, String.valueOf(yzm));
		map.put("flag", result); //发送成功时，返回0
		return map;
	}



	/**
	 * 发送修改密码手机验证码
	 * @param stuMobile
	 * @param session
	 * @return
	 */
	@RequestMapping("forgetSendYzm")
	@ResponseBody
	public Map<String, Object> forgetSendYzm(String stuMobile, HttpSession session){
		Map<String, Object> map = new HashMap<String, Object>();
		Random random = new Random();
		int yzm = random.nextInt(899999)+100000;
		session.setAttribute("forgetSendYzm", yzm);
		int result = SendVerificationCode.sendMSM(stuMobile, String.valueOf(yzm));
		map.put("flag", result); //发送成功时，返回0
		return map;
	}


	/**
	 * 确认修改用户登录密码
	 * @param stuMobile：登录手机号
	 * @param password：修改后的密码
	 * @param phoneYzm：手机验证码
	 * @param session
	 * @return
	 */
	@RequestMapping("queryUpdatePass")
	@ResponseBody
	public Map<String, Object> queryUpdatePass(String stuMobile, String password, String phoneYzm, HttpSession session){
		Map<String, Object> map = new HashMap<String, Object>();
		Integer yzm = (Integer) session.getAttribute("forgetSendYzm");
		if(yzm==null){
			map.put("flag", -1);//验证码失效
		}else if(String.valueOf(yzm).equals(phoneYzm)){ //验证码正确
			int result = studentService.updatePass(stuMobile, password);
			session.removeAttribute("forgetSendYzm");//存入是否成功，清空session
			map.put("flag", result);
		}else{
			map.put("flag", -2);//前后台验证码不一致
		}
		return map;
	}

	/**
	 * 退出
	 * @return
	 */
	@RequestMapping("logout")
	@ResponseBody
	public Map<String, Object> logout(HttpSession sessoin){
		Map<String, Object> map = new HashMap<String, Object>();
		sessoin.removeAttribute("studentEntity");
		map.put("flag", 1);
		return map;
	}


	/**
	 * 加载个人信息
	 * @return
	 */
	@RequestMapping("loadPersonInfo")
	@ResponseBody
	public Map<String, Object> loadPersonInfo(HttpSession sessoin){
		Map<String, Object> map = new HashMap<String, Object>();
		StudentEntity student = (StudentEntity) sessoin.getAttribute("studentEntity");
		StudentEntity studentEntity = null;
		if(student != null){
			studentEntity = studentService.findById(student.getStuId());
		}
		map.put("studentEntity", studentEntity);
		return map;
	}


	/**
	 * 保存修改后的学生基本信息
	 * @return
	 */
	@RequestMapping("saveUpdateStudentInfo")
	@ResponseBody
	public Map<String, Object> saveUpdateStudentInfo(StudentEntity studentEntity, HttpSession sessoin){
		Map<String, Object> map = new HashMap<String, Object>();
		int result = studentService.saveUpdateStudent(studentEntity);
		map.put("flag", result);
		return map;
	}


}
